package com.mytijian.runtime;

import com.mytijian.WebExceptionEnum;
import com.mytijian.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RequestIdCheckHelper {
	
	private Logger log = LoggerFactory.getLogger(RequestIdCheckHelper.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
	public void checkRequestId(String requestId) {
		boolean existFlag= stringRedisTemplate.opsForValue().setIfAbsent(requestId, requestId);
		if(!existFlag){
			log.warn("requestId["+requestId+"]请求已经被处理过了");
			throw new BizException(WebExceptionEnum.REQUESTID_USED);
		}
		stringRedisTemplate.expire(requestId, 5, TimeUnit.SECONDS);
	}

}
